document.addEventListener('DOMContentLoaded', function() {
    const track = document.querySelector('.carousel-track');
    const slides = Array.from(track.children);
    const nextButton = document.querySelector('#nextBtn');
    const prevButton = document.querySelector('#prevBtn');
    const dotsNav = document.querySelector('.bottom-8');
    const dots = Array.from(dotsNav.children);

    const slideWidth = slides[0].getBoundingClientRect().width;

    // 排列幻灯片
    slides.forEach((slide, index) => {
        slide.style.left = slideWidth * index + 'px';
    });

    let currentSlide = 0;
    const slidesCount = slides.length;

    // 更新指示器
    function updateDots() {
        dots.forEach((dot, index) => {
            dot.classList.toggle('opacity-100', index === currentSlide);
            dot.classList.toggle('opacity-50', index !== currentSlide);
        });
    }

    // 移动到指定幻灯片
    function moveToSlide(index) {
        track.style.transform = `translateX(-${slideWidth * index}px)`;
        currentSlide = index;
        updateDots();
    }

    // 自动播放
    let autoplayInterval = setInterval(() => {
        let nextIndex = (currentSlide + 1) % slidesCount;
        moveToSlide(nextIndex);
    }, 5000);

    // 点击下一张
    nextButton.addEventListener('click', () => {
        clearInterval(autoplayInterval);
        let nextIndex = (currentSlide + 1) % slidesCount;
        moveToSlide(nextIndex);
        autoplayInterval = setInterval(() => {
            nextIndex = (currentSlide + 1) % slidesCount;
            moveToSlide(nextIndex);
        }, 5000);
    });

    // 点击上一张
    prevButton.addEventListener('click', () => {
        clearInterval(autoplayInterval);
        let prevIndex = (currentSlide - 1 + slidesCount) % slidesCount;
        moveToSlide(prevIndex);
        autoplayInterval = setInterval(() => {
            let nextIndex = (currentSlide + 1) % slidesCount;
            moveToSlide(nextIndex);
        }, 5000);
    });

    // 点击指示器
    dots.forEach((dot, index) => {
        dot.addEventListener('click', () => {
            clearInterval(autoplayInterval);
            moveToSlide(index);
            autoplayInterval = setInterval(() => {
                let nextIndex = (currentSlide + 1) % slidesCount;
                moveToSlide(nextIndex);
            }, 5000);
        });
    });

    // 初始化第一个指示器
    updateDots();
}); 